var userGauge;

var store = Ext.create('Ext.data.JsonStore', {
    fields: ['data'],
    data: [
        { 'value':30 }
    ]
});

Ext.define('Mobileagri.controller.Contacts', {
    extend: 'Ext.app.Controller',

    stores: ['Contacts', 'Users', 'Messages', 'StatUser'],

    models: ['Contact', 'User', 'Message'],

    views: ['Login', 'contact.Edit', 'contact.List', 'contact.userList', 'contact.messageList', 'contact.userGauge'],

    refs: [{
            ref: 'contactsPanel',
            selector: 'panel'
        },{
            ref: 'contactlist',
            selector: 'contactlist'
        }
    ],

    init: function() {
        this.control({
        	'login button[action=login]': {
            	click: this.login
            },
            'contactlist dataview': {
                itemdblclick: this.editUser
            },
            'userlist dataview': {
                itemdblclick: this.showUserGauge
            },
            'contactlist button[action=add]': {
            	click: this.editUser
            },
            'contactlist button[action=showListMessage]': {
            	click: this.showListMessage
            },
            'contactlist button[action=delete]': {
                click: this.deleteUser
            },
            'contactedit button[action=save]': {
                click: this.updateUser
            },
            'messageList image[action=lireMsg]': {
                click: this.lireMsg
            }
        });
        
//        var edit = Ext.create('Mobileagri.view.contact.userList').show();
//        userGauge = Ext.create('Mobileagri.view.contact.userGauge').show();
//        userGauge.hide();
    },
    
    login: function(button) {
        
       var login = Ext.getCmp('login').getValue();
       var pass = Ext.getCmp('pass').getValue();
       
       if(login=="romain" && pass=="test"){
        
	        var edit2 = Ext.create('Mobileagri.view.contact.List');
	        
	        Ext.getCmp('idLogin').hide();
	        Ext.getCmp('containerId').add(edit2);
	        
//	        userGauge = Ext.create('Mobileagri.view.contact.userGauge').show();
//	      userGauge.hide();
	        var edit = Ext.create('Mobileagri.view.contact.userList').show();
	        
        
       }else{
    	   Ext.Msg.show({
    		     title:'Alert',
    		     msg: 'Mauvaise identification',
    		     buttons: Ext.Msg.OK,
    		     icon: Ext.Msg.ERROR
    		});
       }
        
//      var edit = Ext.create('Mobileagri.view.contact.userList').show();
//      userGauge = Ext.create('Mobileagri.view.contact.userGauge').show();
//      userGauge.hide();
    },

    editUser: function(grid, record) {
        var edit = Ext.create('Mobileagri.view.contact.Edit').show();
        
        if(record){
        	edit.down('form').loadRecord(record);
        }
    },
    
    showUserGauge: function(grid, record) {
    	alert("record:"+record.get('login'));
    	userGauge = Ext.create('Mobileagri.view.contact.userGauge');
    	var comp = userGauge.getComponent("idChart");
    	
//    	var gauge = comp.getComponent("idGauge");
    	
    	comp.bindStore(store );
//    	alert(comp);
    	userGauge.show();
//    	userGauge.show();
//    	userGauge.setHidden(false);
//        
//        if(record){
//        	edit.down('form').loadRecord(record);
//        }
//    	alert(gauge.getTitle());
//    	gauge.setTitle(record.get('login'));
    	
//    	var p = Ext.create('Ext.data.proxy.Proxy', {
//    		type: 'ajax',
//            api: {
//            	read : 'rest/stat/getAvancement',
//                create : 'contact/create.action',
//                update: 'contact/update.action',
//                destroy: 'contact/delete.action'
//            },
//            reader: {
//                type: 'json',
//                root: 'data',
//            },
//            writer: {
//                type: 'json',
//                writeAllFields: true,
//                encode: false,
//                root: 'data'
//            },
//            listeners: {
//                exception: function(proxy, response, operation){
//                    Ext.MessageBox.show({
//                        title: 'REMOTE EXCEPTION',
//                        msg: operation.getError(),
//                        icon: Ext.MessageBox.ERROR,
//                        buttons: Ext.Msg.OK
//                    });
//                }
//            }
//    	});
//    	
//    	store.setProxy(p);
//    	store.sync();
//    	
//    	store.removeAll();
//    	store.add({data: 90});
    	
    	comp.bindStore(store);
    },
    
    showListMessage: function(button) {
        var edit = Ext.create('Mobileagri.view.contact.messageList').show();
    },
    
    lireMsg: function(grid, rowIndex, colIndex) {
    	
    	var storeMsg = this.getMessagesStore();
       var rec = storeMsg.getAt(rowIndex);
       alert(rec.get('id'));
//       store.remove(record);
//	    this.getContactsStore().sync();
       
    },
    
    updateUser: function(button) {
        var win    = button.up('window'),
            form   = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();
        
        
		if (values.id > 0){
			record.set(values);
		} else{
			record = Ext.create('Mobileagri.model.Contact');
			record.set(values);
			record.setId(0);
			this.getContactsStore().add(record);
		}
        
		win.close();
        this.getContactsStore().sync();
    },
    
    deleteUser: function(button) {
    	
    	var grid = this.getContactlist(),
    	record = grid.getSelectionModel().getSelection(), 
        store = this.getContactsStore();

	    store.remove(record);
	    this.getContactsStore().sync();
    }
});
